package com.liu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.domain.base.BaseResult;
import com.liu.domain.base.PageResult;
import com.liu.domain.po.WellPO;
import com.liu.domain.request.ReqWell;
import com.liu.service.WellService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 井信息管理
 *
 * @author LIU
 */
@Tag(name="井管理")
@RestController
@RequestMapping(value = "well")
public class WellController {
    @Autowired
    private WellService wellService;

    /**
     * 井列表
     *
     * @return
     */
    @Operation(description="井列表")
    @PostMapping(value = "page")
    public PageResult<WellPO> getWellPage(@RequestBody ReqWell reqWell) {
        Page<WellPO> pageData = wellService.page(reqWell.page());
        return PageResult.success(pageData.getRecords(), pageData.getTotal());
    }


    /**
     * 保存井
     *
     * @param model 井信息请求对象
     * @return 是否保存成功
     */
    @Operation(description="保存井")
    @PostMapping(value = "create-well")
    public BaseResult createWell(@Valid @RequestBody WellPO model) {
        wellService.save(model);
        return BaseResult.success();
    }

    /**
     * 更新井
     *
     * @return
     */
    @PostMapping(value = "update")
    public BaseResult updateWell(@RequestBody WellPO model) {
        boolean flag = wellService.updateById(model);
        return flag ? BaseResult.success() : BaseResult.error("操作失败");
    }


    /**
     * 删除井
     *
     * @param id 井ID
     * @return
     */
    @PostMapping(value = "delete")
    public BaseResult deleteWell(@RequestParam Long id) {
        boolean flag = wellService.removeById(id);
        return flag ? BaseResult.success() : BaseResult.error("操作失败");
    }
}
